package com.sita.bike.ui.fragments;

import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyTrafficStyle;
import com.sita.bike.R;
import com.sita.bike.support.Constants;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.support.LocationController;
import com.sita.bike.utils.BitmapUtils;
import com.sita.bike.utils.LogUtils;

/**
 * Class BasicMapFragment contains own location marker
 *
 * @author markman on 16/02/15.
 */
public class BasicMapFragment extends Fragment implements AMapLocationListener {

    private static final String TAG = BasicMapFragment.class.getSimpleName();
    protected AMap aMap;
    protected TextureMapView mapView;
    protected Marker myMarker;
    protected UiSettings mUiSettings;
    protected boolean mIsStartLocationClient = false;
    protected boolean mIsFollowingMode = false;

    public BasicMapFragment() {
        if (mIsStartLocationClient) {
            LocationController.startNormal(this);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = hasCreatedView(inflater, container, savedInstanceState);
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_baisc_map, container, false);
            mapView = (TextureMapView) view.findViewById(R.id.basic_map);
        }

        mapView.onCreate(savedInstanceState);// 此方法必须重写
        initMap();
        return view;

    }

    protected View hasCreatedView(LayoutInflater inflater, ViewGroup container,
                                  Bundle savedInstanceState) {
        return null;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mapView != null) {
            mapView.onResume();
            initMap();
        }

    }

    @Override
    public void onPause() {
        if (mapView != null) {
            mapView.onPause();
        }
        super.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mapView != null) {
            mapView.onSaveInstanceState(outState);
        }
    }

    @Override
    public void onDestroy() {
        if (mapView != null) {
            mapView.onDestroy();
            aMap = null;
        }
        super.onDestroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mapView != null) {
            mapView.onDestroy();
            aMap = null;
        }
    }

    private void initMap() {

        if (aMap == null) {
            aMap = mapView.getMap();
            setMapUi();
        }
        setTraffic();

        AMapLocation location = LocationController.getLastKnownLocation();
        LatLng latLng;
        if (location != null) {
            latLng = new LatLng(location.getLatitude(), location.getLongitude());
            drawMyMarker(latLng);
        } else {
            latLng = Constants.DEFAULT_LATLNG;
        }
        moveCamera(latLng);

    }

    private void moveCamera(LatLng latLng) {
        CameraPosition cameraPosition;
        cameraPosition = new CameraPosition.Builder()
                .target(latLng).zoom(Constants.DEFAULT_ZOOM_LEVEL).bearing(0).tilt(30).build();
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(cameraPosition);
        aMap.moveCamera(update);
    }

    private void setMapUi() {
        mUiSettings = aMap.getUiSettings();
        mUiSettings.setZoomControlsEnabled(true);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);
        mUiSettings.setScrollGesturesEnabled(true);
    }

    private void setTraffic() {
        MyTrafficStyle myTrafficStyle = new MyTrafficStyle();
        myTrafficStyle.setSeriousCongestedColor(0xff92000a);
        myTrafficStyle.setCongestedColor(0xffea0312);
        myTrafficStyle.setSlowColor(0xffff7508);
        myTrafficStyle.setSmoothColor(0xff00a209);
        aMap.setTrafficEnabled(false);
    }

    /*
     * get own location
     */
    @Override
    public void onLocationChanged(AMapLocation location) {
        LogUtils.d(TAG, "onLocationChanged: ");
        if (mIsStartLocationClient) {
            if (myMarker != null) {
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                drawMyMarker(latLng);
                if (mIsFollowingMode) {
                    moveCamera(latLng);
                }
            }
        }
    }

    /*
     * draw own marker
     */
    protected void drawMyMarker(LatLng latLng) {
        if (myMarker != null && !myMarker.isVisible()) {
            myMarker.destroy();
            myMarker = null;
        }

        if (myMarker == null) {
            Bitmap locationMe= BitmapUtils.zoomBitmap(BitmapFactory.decodeResource(GlobalContext.getGlobalContext().getResources(), R.drawable.location_me), 100, 100);
            BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(locationMe);
            myMarker = aMap.addMarker(new MarkerOptions().draggable(false).icon(icon).position(latLng));
        } else {
            myMarker.setPosition(latLng);
        }

    }

    public void startFollowingMode() {
        mIsStartLocationClient = true;
        mIsFollowingMode = true;
    }

    public void stopFollowingMode() {
        mIsStartLocationClient = false;
        mIsFollowingMode = false;
    }

}
